﻿using UnityEngine;using UnityEditor;
using System.IO;
using System.Text;public class MyEditor : EditorWindow
{
    bool[] buildAssetBundleOptions = new bool[7];
    BuildTarget[] buildTargets = new BuildTarget[] { BuildTarget.iOS, BuildTarget.Android, BuildTarget.StandaloneWindows };
    int buildTarget = 2;

    [MenuItem("Custom/Build AssetBundles Window")]
    static void AddWindow()
    {
        //创建窗口
        Rect wr = new Rect(0, 0, 640, 640);
        MyEditor window = (MyEditor)EditorWindow.GetWindowWithRect(typeof(MyEditor), wr, true, "window");
        window.Show();

    }

    string[] names = new string[3] { "a", "unity3d", "/StreamingAssets" };
    BuildAssetBundleOptions[] babos = new BuildAssetBundleOptions[]{                BuildAssetBundleOptions.None,                BuildAssetBundleOptions.UncompressedAssetBundle,                BuildAssetBundleOptions.DisableWriteTypeTree ,                BuildAssetBundleOptions.DeterministicAssetBundle ,                BuildAssetBundleOptions.ForceRebuildAssetBundle ,                BuildAssetBundleOptions.IgnoreTypeTreeChanges ,                BuildAssetBundleOptions.AppendHashToAssetBundleName

        };


    //绘制窗口时调用
    void OnGUI()
    {
        GUILayout.Space(20);
        //输入框控件
        names[0] = EditorGUILayout.TextField("assetBundleName:", names[0]);
        names[1] = EditorGUILayout.TextField("assetBundleVariant:", names[1]);
        names[2] = EditorGUILayout.TextField("assetPath:", names[2]);

        GUILayout.Space(50);
        GUILayout.Button("buildAssetBundleOptions", GUILayout.Width(200));
        for (int i = 0; i < buildAssetBundleOptions.Length; i++)
        {
            buildAssetBundleOptions[i] = GUILayout.Toggle(buildAssetBundleOptions[i], babos[i].ToString());
        }

        GUILayout.Space(50);
        GUILayout.Button("build target", GUILayout.Width(200));
        buildTarget = GUILayout.Toolbar(buildTarget, new string[] { "ios", "android", "window" });

        GUILayout.BeginArea(new Rect(160, 560, 350, 100));
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("关闭窗口", GUILayout.Width(150), GUILayout.Height(50)))
        {
            //关闭窗口
            this.Close();
        }
        if (GUILayout.Button("创建资源包", GUILayout.Width(150), GUILayout.Height(50)))
        {

            BuildAssetBundleOptions bbo = BuildAssetBundleOptions.None;

            for (int i = 0; i < buildAssetBundleOptions.Length; i++)
            {
                if (buildAssetBundleOptions[i])
                {
                    if (bbo != BuildAssetBundleOptions.None)
                    {
                        bbo = bbo | babos[i];
                    }
                    else {
                        bbo = babos[i];
                    }
                }
            }
            bool isSucc = ExportAssetBundles5(names[0], names[1], names[2], bbo, buildTargets[buildTarget]);

            this.ShowNotification(new GUIContent(isSucc ? "创建成功" : "创建失败"));
        }
        GUILayout.EndArea();
    }

    public static bool ExportAssetBundles5(string assetBundleName = "a",
                                                string assetBundleVariant = "unity3d",
                                                string assetPath = "/StreamingAssets",
                                                BuildAssetBundleOptions basbOptions = BuildAssetBundleOptions.CollectDependencies,
                                                BuildTarget bTarget = BuildTarget.StandaloneWindows)
    {
        //获取在Project视图中选择的所有游戏对象
        Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        int length = SelectedAsset.Length;
        if (length == 0)
        {
            Debug.LogWarning("none asset is selected");
            return false;
        }
        string[] assetNames = new string[length];
        for (int i = 0; i < length; i++)
        {
            assetNames[i] = AssetDatabase.GetAssetPath(SelectedAsset[i]);
        }
        AssetBundleBuild[] abbs = new AssetBundleBuild[1];
        AssetBundleBuild sbb = new AssetBundleBuild();
        sbb.assetBundleName = assetBundleName;
        sbb.assetBundleVariant = assetBundleVariant;
        sbb.assetNames = AssetDatabase.GetDependencies(assetNames);
        abbs[0] = sbb;
        assetPath = Application.dataPath + assetPath;// +PlatformPath(bTarget);
        if (!Directory.Exists(assetPath))
            Directory.CreateDirectory(assetPath);
        AssetBundleManifest amf = BuildPipeline.BuildAssetBundles(assetPath, abbs, basbOptions, bTarget);
        if (amf != null)
        {
            Debug.Log("create asb succeed ! file path : " + assetPath);
        }
        //刷新编辑器
        AssetDatabase.Refresh();

        return amf != null;
    }

    private static string PlatformPath(BuildTarget target)
    {
        switch (target)
        {
            case BuildTarget.Android:
                return "/Android";
            case BuildTarget.StandaloneWindows:
                return "/Windows";
            case BuildTarget.StandaloneWindows64:
                return "/Windows";
            case BuildTarget.WebPlayer:
                return "/Windows";
            case BuildTarget.iOS:
                return "/IOS";
        }
        return "/Windows";
    }
}